package com.cxg.fairy.ec.main.sort.content;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cxg.fairy.delegates.LatteDelegate;
import com.cxg.fairy.ec.main.EcBottomDelegate;
import com.cxg.fairy.ec.main.search.SearchListDelegate;
import com.cxg.fairy.net.callback.IError;
import com.cxg.fairy.ui.banner.BannerCreator;
import com.cxg.fairy.util.log.LatteLogger;
import com.diabin.latte.ec.R;
import com.diabin.latte.ec.R2;
import com.cxg.fairy.net.RestClient;
import com.cxg.fairy.net.callback.ISuccess;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by CFairy
 */

public class ContentDelegate extends LatteDelegate implements OnItemClickListener{

    private static final String ARG_CONTENT_ID = "CONTENT_ID";
    private int mContentId = -1;
    private List<SectionBean> mData = null;

    @BindView(R2.id.rv_list_content)
    RecyclerView mRecyclerView = null;

    @BindView(R2.id.content_header_banner)
    ConvenientBanner<String> content_banner = null;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Bundle args = getArguments();

        if (args != null) {
            mContentId = args.getInt(ARG_CONTENT_ID);
        }
    }

    public static ContentDelegate newInstance(int contentId) {
        final Bundle args = new Bundle();
        args.putInt(ARG_CONTENT_ID, contentId);
        final ContentDelegate delegate = new ContentDelegate();
        delegate.setArguments(args);
        return delegate;
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_list_content;
    }

    private void initData() {
        RestClient.builder()
                .url("getCategoryByParentId_otherLevel")
                .params("parentId", mContentId+"")
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        final JSONObject obj1 = JSON.parseObject(response);
                        final JSONArray obj2 = obj1.getJSONArray("banners");
                        initBanner(obj2);
//                   // E/RecyclerView: No adapter attached; skipping layout
                        mData = new SectionDataConverter().convert(response);
                        final SectionAdapter sectionAdapter = new SectionAdapter(R.layout.item_section_content, R.layout.item_section_header, mData);

                        sectionAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                            @Override
                            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                                SectionBean sectionBean = (SectionBean) adapter.getData().get(position);
                                //二级大类
                                if (sectionBean.isMore()) {
                                    final int sectionId = sectionBean.getSectionId();
                                    final String sectionName = sectionBean.header;
                                    //final GoodsDetailDelegate delegate = GoodsDetailDelegate.create(sectionId);
                                    final SearchListDelegate delegate = SearchListDelegate.create(sectionName);
                                    getParentDelegate().getParentDelegate().getSupportDelegate().start(delegate);
                                } else {
                                    //三级大类

                                }

                            }
                        });

                        mRecyclerView.setAdapter(sectionAdapter);
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                    }
                })
                .build()
                .post();
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        final StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(manager);
        final EcBottomDelegate ecBottomDelegate = getParentDelegate().getParentDelegate();
        mRecyclerView.addOnItemTouchListener(ContentItemClickListener.create(ecBottomDelegate));
        initData();
    }

    private void initBanner(JSONArray data) {
        final int size = data.size();
        if (size>0) {
            final ArrayList<String> images = new ArrayList<>();

            for (int i = 0; i < size; i++) {
                images.add(((JSONObject) data.get(i)).get("sku_url").toString());
                BannerCreator.setDefault(content_banner, images, this);
            }
        } else {

        }
    }

    @Override
    public void onItemClick(int position) {
        Toast.makeText(getContext(), "position：" + position, Toast.LENGTH_SHORT).show();
    }
}
